<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Function 对象 | JavaScript 高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.2.0">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
    

        
    
    
    <link rel="next" href="../e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html" />
    
    
    <link rel="prev" href="../e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="1.5" data-basepath=".." data-revision="Tue Mar 01 2016 06:46:18 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        JavaScript 高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="e9c9c586570ff813bf127694b025d155/index.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        面向对象基础复习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        原型的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        继承的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        Object 对象
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.5" data-path="e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        Function 对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        作用域链
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        闭包的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        getter 与 setter
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="e2bb52a93f2a04d3848c6bc059d4442d/index.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        面向对象高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        原型链
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        函数的四中调用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        对象的创建模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        继承的方式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        闭包案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        正则表达式对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        XMLHttpRequest 对象
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >JavaScript 高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="fountion-%E5%AF%B9%E8%B1%A1">Fountion &#x5BF9;&#x8C61;</h1>
<p>&#x4E3B;&#x8981;&#x5185;&#x5BB9;:</p>
<ul>
<li>&#x51FD;&#x6570;&#x4E5F;&#x662F;&#x5BF9;&#x8C61;</li>
<li>&#x51FD;&#x6570;&#x7684;&#x533F;&#x540D;&#x8868;&#x8FBE;&#x5F0F;</li>
<li>&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;</li>
<li>&#x51FD;&#x6570;&#x540D;&#x63D0;&#x5347;</li>
<li>eval &#x4E0E; Function</li>
</ul>
<p>JavaScript &#x4E2D;, &#x51FD;&#x6570;&#x662F;&#x4E00;&#x7B49;&#x516C;&#x6C11;. &#x5B9E;&#x9645;&#x4E0A;&#x6240;&#x6709;&#x7684;&#x4E1C;&#x897F;&#x90FD;&#x9700;&#x8981;&#x4F9D;&#x9644;&#x4E8E;&#x51FD;&#x6570;. &#x524D;&#x9762;&#x5DF2;&#x7ECF;&#x5B66;&#x4E60;&#x8FC7;&#x51FD;&#x6570;&#x7684;&#x57FA;&#x672C;&#x7528;&#x6CD5;, &#x63A5;&#x4E0B;&#x6765;&#x8BE6;&#x7EC6;&#x8BA8;&#x8BBA;&#x4E00;&#x4E0B;&#x51FD;&#x6570;&#x7684;&#x66F4;&#x6DF1;&#x5C42;&#x6B21;&#x7684;&#x5185;&#x5BB9;.</p>
<h2 id="%E5%87%BD%E6%95%B0%E4%B9%9F%E6%98%AF%E4%B8%80%E4%B8%AA%E5%AF%B9%E8%B1%A1">&#x51FD;&#x6570;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;</h2>
<p>&#x5728; JavaScript &#x4E2D;&#x51FD;&#x6570;&#x4E5F;&#x662F;&#x4E00;&#x79CD;&#x6570;&#x636E;&#x7C7B;&#x578B;.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{}
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> foo); <span class="hljs-comment">// =&gt; function</span>
</code></pre>
<p>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;, &#x51FD;&#x6570;&#x7684;&#x5730;&#x4F4D;&#x4E0E;&#x5BF9;&#x8C61;&#x7684;&#x5730;&#x4F4D;&#x662F;&#x4E00;&#x6837;&#x7684;, &#x4E0E;&#x6570;&#x5B57; <code>123</code>, &#x5B57;&#x7B26;&#x4E32; <code>&quot;abc&quot;</code> &#x4E00;&#x6837;, &#x662F;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x79CD;&#x6570;&#x636E;. &#x56E0;&#x6B64;&#x4E5F;&#x5C31;&#x6709;&#x4E86;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x6982;&#x5FF5; (&#x540E;&#x9762;&#x4ECB;&#x7ECD;).</p>
<h3 id="-function-%E5%87%BD%E6%95%B0"><code>Function</code> &#x51FD;&#x6570;</h3>
<p>&#x5728; JavaScript &#x4E2D;, &#x6709;&#x4E00;&#x4E2A; <code>Function</code> &#x51FD;&#x6570;, &#x5B83;&#x662F;&#x6240;&#x6709;&#x51FD;&#x6570;&#x7684;&#x7C7B;&#x578B;, &#x6BCF;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x90FD;&#x662F; <code>Function</code> &#x7684;&#x5B9E;&#x4F8B;. &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x51FD;&#x6570;, &#x9664;&#x4E86;&#x4F7F;&#x7528; <code>function</code> &#x5173;&#x952E;&#x5B57;&#x5B9A;&#x4E49;, &#x540C;&#x6837;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>Function</code> &#x6765;&#x8868;&#x793A;, &#x4E24;&#x79CD;&#x8868;&#x793A;&#x65B9;&#x6CD5;&#x7B49;&#x4EF7;.</p>
<p>&#x5B9A;&#x4E49; <code>Function</code> &#x7684;&#x8BED;&#x6CD5;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>(&#x53C2;&#x6570;<span class="hljs-number">1</span>, &#x53C2;&#x6570;<span class="hljs-number">2</span>, ..., &#x65B9;&#x6CD5;&#x4F53;);
</code></pre>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x8BED;&#x6CD5;&#x7ED3;&#x6784;&#x4E2D;, &#x6784;&#x9020;&#x51FD;&#x6570; <code>Function</code> &#x7684;&#x53C2;&#x6570;&#x81F3;&#x5C11;&#x6709;&#x4E00;&#x4E2A;, &#x53C2;&#x6570;&#x90FD;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;. &#x591A;&#x4E2A;&#x53C2;&#x6570;&#x5206;&#x522B;&#x8868;&#x793A;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x65F6;&#x7684;&#x53C2;&#x6570;&#x4E0E;&#x65B9;&#x6CD5;&#x4F53;.</p>
<p>&#x4F8B;&#x5982;, &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x52A0;&#x6CD5;&#x7684;&#x51FD;&#x6570;:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">func</span> (<span class="hljs-params">num1, num2</span>) </span>{
    <span class="hljs-keyword">return</span> num1 + num2;
}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x6709;&#x4E24;&#x4E2A;&#x53C2;&#x6570;, &#x5206;&#x522B;&#x662F; <code>num1</code>, &#x548C; <code>num2</code>. &#x65B9;&#x6CD5;&#x4F53;&#x662F; <code>return num1 + num2;</code>. &#x56E0;&#x6B64;&#x5C06;&#x5176;&#x6539;&#x5199;&#x4E3A; <code>Function</code> &#x7684;&#x5F62;&#x5F0F;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>(<span class="hljs-string">&quot;num1&quot;</span>, <span class="hljs-string">&quot;num2&quot;</span>, <span class="hljs-string">&quot;return num1 + num2&quot;</span>;);
</code></pre>
<p>&#x8C03;&#x7528;&#x51FD;&#x6570;&#x76F4;&#x63A5;&#x4F7F;&#x7528; <code>func</code> &#x5373;&#x53EF;:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(func(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)); <span class="hljs-comment">// =&gt; 3</span>
</code></pre>
<p>&#x53EF;&#x89C1;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x4E0E;&#x76F4;&#x63A5;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x4E00;&#x6837;.</p>
<p><strong>&#x7EC3;&#x4E60;</strong> &#x6539;&#x5199;&#x4E0B;&#x9762;&#x51FD;&#x6570;&#x4E3A; <code>Function</code> &#x5F62;&#x5F0F;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x6C42;&#x6700;&#x5927;&#x503C;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">max</span>(<span class="hljs-params">num1, num2, num3</span>) </span>{
    <span class="hljs-keyword">var</span> max;
    <span class="hljs-keyword">if</span> (num1 &gt;= num2) {
        <span class="hljs-keyword">if</span> (num1 &gt;= num3) {
            max = num1;
        } <span class="hljs-keyword">else</span> {
            max = num3;
        }
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">if</span> (num2 &gt;= num3) {
            max = num2;
        } <span class="hljs-keyword">else</span> {
            max = num3;
        }
    }
    <span class="hljs-keyword">return</span> max;
}
</code></pre>
<h3 id="%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0%E6%98%AF-function-%E7%9A%84%E5%AF%B9%E8%B1%A1">&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x662F; <code>Function</code> &#x7684;&#x5BF9;&#x8C61;</h3>
<p>&#x53EF;&#x89C1; <code>new Function(...)</code> &#x540E;&#x5F97;&#x5230;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;. &#x5B9E;&#x9645;&#x4E0A;, &#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;, &#x662F; <code>Function</code> &#x7684;&#x5B9E;&#x4F8B;.</p>
<p>&#x5728; JavaScript &#x4E2D;&#x8981;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x4E3A;&#x67D0;&#x4E2A;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;, &#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>instanceof</code> &#x8FD0;&#x7B97;&#x7B26;, &#x5176;&#x8BED;&#x6CD5;&#x4E3A;:</p>
<pre><code class="lang-javascript">&#x5B9E;&#x4F8B; <span class="hljs-keyword">instanceof</span> &#x6784;&#x9020;&#x51FD;&#x6570;
</code></pre>
<p>&#x5982;&#x679C;&#x5B9E;&#x4F8B;&#x662F;&#x88AB;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;, &#x8BE5;&#x8868;&#x8FBE;&#x5F0F;&#x8FD4;&#x56DE; <code>true</code>; &#x5982;&#x679C;&#x5B9E;&#x4F8B;&#x662F;&#x7EE7;&#x627F;&#x81EA;&#x6784;&#x9020;&#x51FD;&#x6570;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;&#x5BF9;&#x8C61;&#x7684;, &#x8868;&#x8FBE;&#x5F0F;&#x8FD4;&#x56DE; <code>true</code>. &#x5426;&#x5219;&#x8FD4;&#x56DE; <code>false</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo1</span>(<span class="hljs-params"></span>) </span>{}
<span class="hljs-keyword">var</span> o1 = <span class="hljs-keyword">new</span> foo1();
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo2</span>(<span class="hljs-params"></span>) </span>{}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo3</span>(<span class="hljs-params"></span>) </span>{};
foo3.prototype = o1; <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x539F;&#x578B;</span>
<span class="hljs-keyword">var</span> o2 = <span class="hljs-keyword">new</span> foo3(); <span class="hljs-comment">// o2 &#x7EE7;&#x627F;&#x81EA; o1</span>

<span class="hljs-built_in">console</span>.log(o2 <span class="hljs-keyword">instanceof</span> foo1); <span class="hljs-comment">// =&gt; true</span>
<span class="hljs-built_in">console</span>.log(o2 <span class="hljs-keyword">instanceof</span> foo2); <span class="hljs-comment">// =&gt; false</span>
<span class="hljs-built_in">console</span>.log(o2 <span class="hljs-keyword">instanceof</span> foo3); <span class="hljs-comment">// =&gt; true</span>
</code></pre>
<p>&#x4F7F;&#x7528; <code>instanceof</code> &#x8FD0;&#x7B97;&#x7B26;, &#x53EF;&#x4EE5;&#x9A8C;&#x8BC1;&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x662F;&#x5426;&#x4E3A; <code>Function</code> &#x7684;&#x5B9E;&#x4F8B;:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo1</span>(<span class="hljs-params"></span>) </span>{}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo2</span>(<span class="hljs-params"></span>) </span>{}
<span class="hljs-built_in">console</span>.log(foo1 <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Function</span>); <span class="hljs-comment">// =&gt; true</span>
<span class="hljs-built_in">console</span>.log(foo1 <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Array</span>); <span class="hljs-comment">// =&gt; false</span>
<span class="hljs-built_in">console</span>.log(foo1 <span class="hljs-keyword">instanceof</span> foo2); <span class="hljs-comment">// =&gt; false</span>
</code></pre>
<h3 id="%E5%87%BD%E6%95%B0%E7%9A%84%E5%AD%97%E9%9D%A2%E9%87%8F">&#x51FD;&#x6570;&#x7684;&#x5B57;&#x9762;&#x91CF;</h3>
<p>&#x51FD;&#x6570;&#x5728;&#x4F7F;&#x7528;&#x7684;&#x65F6;&#x5019;&#x4E5F;&#x53EF;&#x4EE5;&#x52A8;&#x6001;&#x7684;&#x8D4B;&#x503C;, &#x4F7F;&#x7528;&#x7684;&#x4FBF;&#x662F;&#x51FD;&#x6570;&#x7684;&#x5B57;&#x9762;&#x91CF;. &#x5176;&#x8BED;&#x6CD5;&#x4E3A;:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">&#x53C2;&#x6570;, ...</span>) </span>{
    &#x51FD;&#x6570;&#x4F53;;
}
</code></pre>
<p>&#x51FD;&#x6570;&#x7684;&#x5B57;&#x9762;&#x91CF;, &#x548C;&#x6570;&#x5B57;&#x5B57;&#x9762;&#x91CF;, &#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;, &#x6216;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x4E00;&#x6837;, &#x8868;&#x793A;&#x7684;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x672C;&#x8EAB;. &#x4F7F;&#x7528;&#x7684;&#x4F7F;&#x7528;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6765;&#x63A5;&#x6536;, &#x518D;&#x4F7F;&#x7528;&#x8BE5;&#x53D8;&#x91CF;&#x6765;&#x8C03;&#x7528;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> foo = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&quot;</span>);
};  <span class="hljs-comment">// &#x8D4B;&#x503C;&#x8868;&#x8FBE;&#x5F0F;, &#x9700;&#x8981;&#x4F7F;&#x7528;&#x5206;&#x53F7;&#x7ED3;&#x675F;</span>

foo(); <span class="hljs-comment">// &#x8C03;&#x7528;</span>
</code></pre>
<p>&#x6216;&#x8005;&#x4F7F;&#x7528;&#x81EA;&#x8C03;&#x7528;&#x51FD;&#x6570;:</p>
<pre><code class="lang-javascript">(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x81EA;&#x8C03;&#x7528;&#x51FD;&#x6570;&quot;</span>);
})();
</code></pre>
<p><em>&#x6CE8;&#x610F;: &#x4F7F;&#x7528;&#x51FD;&#x6570;&#x5B57;&#x9762;&#x91CF;&#x7684;&#x65F6;&#x5019;, &#x4F7F;&#x7528;&#x7684;&#x662F;&#x8D4B;&#x503C;&#x8BED;&#x53E5;, &#x56E0;&#x6B64;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x5206;&#x53F7;&#x7ED3;&#x5C3E;.</em></p>
<h2 id="-function-%E7%9A%84%E5%8E%9F%E5%9E%8B"><code>Function</code> &#x7684;&#x539F;&#x578B;</h2>
<p>&#x7C7B;&#x4F3C;&#x4E8E;&#x5BF9;&#x8C61;&#x548C; <code>Object.prototype</code> &#x7684;&#x5173;&#x7CFB;&#x4E00;&#x6837;. &#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x662F; <code>Function</code> &#x7684;&#x5B9E;&#x4F8B;. &#x56E0;&#x6B64;&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x975E;&#x6B63;&#x5F0F;&#x5C5E;&#x6027; <code>__proto__</code> &#x4E0E; <code>Function.prototype</code> &#x7684;&#x5F15;&#x7528;&#x76F8;&#x540C;:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo1</span>(<span class="hljs-params"></span>) </span>{}
<span class="hljs-keyword">var</span> foo2 = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{};
<span class="hljs-built_in">console</span>.log(foo1.__proto__ === <span class="hljs-built_in">Function</span>.prototype); <span class="hljs-comment">// =&gt; true</span>
<span class="hljs-built_in">console</span>.log(foo2.__proto__ === <span class="hljs-built_in">Function</span>.prototype); <span class="hljs-comment">// =&gt; true</span>
</code></pre>
<p>&#x56E0;&#x6B64; <code>Function.prototype</code> &#x5C31;&#x662F;&#x4EFB;&#x610F;&#x51FD;&#x6570;&#x7684;&#x539F;&#x578B;. &#x5947;&#x602A;&#x7684;&#x662F; <code>Function</code> &#x4E5F;&#x662F;&#x51FD;&#x6570;, &#x56E0;&#x6B64; <code>Function.__proto__</code> &#x4E0E; <code>Function.prototype</code> &#x5F15;&#x7528;&#x4E5F;&#x76F8;&#x7B49;.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Function</span>.__proto__ === <span class="hljs-built_in">Function</span>.prototype);
</code></pre>
<p><span style="color: red; font-weight:bolder;">&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4EFB;&#x610F;&#x51FD;&#x6570;&#x90FD;&#x7EE7;&#x627F;&#x81EA; <code>Function.prototype</code> &#x5BF9;&#x8C61;.</span></p>
<p><img src="../images/Snip20160224_29.png" alt=""></p>
<p>&#x56E0;&#x6B64;&#x5982;&#x679C;&#x9700;&#x8981;&#x7ED9;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x90FD;&#x6DFB;&#x52A0;&#x65B9;&#x6CD5;, &#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x8BED;&#x6CD5;:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">Function</span>.prototype.showName = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-regexp">/function\s(.+?)\(/</span>.exec(<span class="hljs-keyword">this</span>.toString())[<span class="hljs-number">1</span>]);
};

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">jkmethod</span>(<span class="hljs-params"></span>) </span>{}
jkmethod.showName();
</code></pre>
<p>&#x7ED9; <code>Function.prototype</code> &#x6DFB;&#x52A0;&#x7684;&#x4EFB;&#x4F55;&#x6210;&#x5458;, &#x90FD;&#x4F1A;&#x88AB;&#x7EE7;&#x627F;&#x5230;&#x4EFB;&#x610F;&#x51FD;&#x6570;&#x4E2D;. &#x56E0;&#x6B64;&#x5728; Douglas &#x7684; JavaScript: The Good Parts &#x4E2D;&#x6709;&#x4E0B;&#x9762;&#x7684;&#x7ECF;&#x5178;&#x4EE3;&#x7801; (&#x540E;&#x9762;&#x89E3;&#x91CA;):</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">Function</span>.prototype.method = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">name, func</span>) </span>{
    <span class="hljs-keyword">this</span>.prototype[name] = func;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
} <span class="hljs-comment">// &#x4EFB;&#x610F;&#x65B9;&#x6CD5;&#x90FD;&#x6709; method &#x65B9;&#x6CD5;, &#x7ED9;&#x5F53;&#x524D;&#x65B9;&#x6CD5;&#x7684;&#x539F;&#x578B;&#x6DFB;&#x52A0;&#x65B0;&#x65B9;&#x6CD5;</span>
</code></pre>
<h2 id="%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0">&#x51FD;&#x6570;&#x53C2;&#x6570;</h2>
<p>&#x51FD;&#x6570;&#x5728;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;, &#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x7684;&#x590D;&#x7528;, &#x9700;&#x8981;&#x7ED9;&#x5176;&#x4F20;&#x9012;&#x53C2;&#x6570;. &#x5728;&#x672C;&#x8282;&#x4E2D;&#x4E3B;&#x8981;&#x8BA8;&#x8BBA;&#x4E09;&#x4E2A;&#x8BDD;&#x9898;: &#x5F62;&#x53C2;&#x4E0E;&#x5B9E;&#x53C2;&#x7684;&#x6982;&#x5FF5;, &#x6CA1;&#x6709;&#x51FD;&#x6570;&#x91CD;&#x8F7D;, &#x4EE5;&#x53CA; <code>arguments</code> &#x5BF9;&#x8C61;.</p>
<h3 id="%E5%BD%A2%E5%8F%82%E4%B8%8E%E5%AE%9E%E5%8F%82">&#x5F62;&#x53C2;&#x4E0E;&#x5B9E;&#x53C2;</h3>
<p>&#x5728;&#x51FD;&#x6570;&#x7684;&#x672F;&#x8BED;&#x4E2D;&#x6709;&#x4E24;&#x4E2A;&#x6982;&#x5FF5;, &#x4E00;&#x4E2A;&#x662F;&#x5F62;&#x53C2;&#x4E00;&#x4E2A;&#x662F;&#x5B9E;&#x53C2;. &#x90A3;&#x4E48;&#x600E;&#x4E48;&#x7406;&#x89E3;&#x5462;? &#x9996;&#x5148;&#x770B;&#x770B;&#x5F62;&#x5F0F;&#x8BA1;&#x7B97;&#x7684;&#x6982;&#x5FF5;.</p>
<p>&#x6240;&#x8C13;&#x7684;&#x5F62;&#x5F0F;&#x8BA1;&#x7B97;, &#x5C31;&#x662F;&#x7B26;&#x53F7;&#x8BA1;&#x7B97;, &#x4E0D;&#x6D89;&#x53CA;&#x5177;&#x4F53;&#x7684;&#x6570;&#x636E;. &#x53D6;&#x800C;&#x4EE3;&#x4E4B;&#x7684;&#x662F;&#x7B26;&#x53F7;&#x7684;&#x89C4;&#x5F8B;&#x4E0E;&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;. &#x4F8B;&#x5982;&#x4E2D;&#x5B66;&#x9636;&#x6BB5;&#x5B66;&#x4E60;&#x7684;&#x4EE3;&#x6570;&#x5C31;&#x662F;&#x5178;&#x578B;&#x7684;&#x5F62;&#x5F0F;&#x8BA1;&#x7B97;. &#x4F8B;&#x5982;</p>
<p>$$
(a + b)^2 = a^2 + 2ab + b^2 \
\int_a^b f(x) \rm{d}x = \left.F(x)\right|_a^b</p>
<p>$$</p>
<p>&#x90A3;&#x4E48;&#x5C06;&#x8FD0;&#x7B97;&#x53EF;&#x4EE5;&#x4ECE;<strong>&#x5177;&#x4F53;&#x7684;&#x6570;&#x636E;</strong>&#x8F6C;&#x5230;<strong>&#x7B26;&#x53F7;&#x903B;&#x8F91;&#x63A8;&#x5BFC;&#x4E2D;</strong>. &#x5C06;&#x7E41;&#x590D;&#x7684;&#x8BA1;&#x7B97;&#x8FC7;&#x7A0B;&#x7B80;&#x5316;&#x6210;&#x51E0;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x516C;&#x5F0F;, &#x7136;&#x540E;&#x5E26;&#x5165;&#x6570;&#x636E;&#x5B8C;&#x6210;&#x8BA1;&#x7B97;. &#x8FD9;&#x4FBF;&#x662F;&#x5F62;&#x5F0F;&#x8BA1;&#x7B97;.</p>
<p>&#x90A3;&#x4E48;&#x5F62;&#x53C2;&#x662F;&#x4EC0;&#x4E48;&#x5462;? &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x51FD;&#x6570;, &#x4F8B;&#x5982;&#x6253;&#x5370;&#x4E00;&#x4E2A;&#x6570;&#x5B57;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">showNum</span>(<span class="hljs-params">num</span>) </span>{
    <span class="hljs-built_in">console</span>.log(num);
}
</code></pre>
<p>&#x51FD;&#x6570;&#x4F53;&#x5C31;&#x50CF;&#x6570;&#x5B66;&#x7B26;&#x53F7;&#x8868;&#x8FBE;&#x5F0F;, &#x6574;&#x4E2A;&#x903B;&#x8F91;&#x5C31;&#x662F;&#x57FA;&#x4E8E;&#x53C2;&#x6570;&#x8FD9;&#x4E2A;&#x7B26;&#x53F7;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x7684;&#x4E00;&#x6BB5;&#x903B;&#x8F91;. &#x6253;&#x5370;&#x7684;&#x662F;&#x53C2;&#x6570; <code>num</code>. &#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x6574;&#x4E2A;&#x51FD;&#x6570;&#x4F53;&#x90FD;&#x662F;&#x56F4;&#x7ED5;&#x53C2;&#x6570;&#x8FD9;&#x4E2A;&#x7B26;&#x53F7;&#x6765;&#x6267;&#x884C;&#x7684;&#x903B;&#x8F91;, &#x4E0D;&#x6D89;&#x53CA;&#x5230;&#x5177;&#x4F53;&#x7684;&#x53C2;&#x4E0E;&#x6570;&#x636E; (&#x51FD;&#x6570;&#x4F53;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x5E38;&#x91CF;&#x6570;&#x636E;&#x4E0D;&#x7B97;), &#x56E0;&#x6B64;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5F62;&#x5F0F;&#x903B;&#x8F91;&#x7ED3;&#x6784;. &#x6240;&#x4EE5;&#x5C06;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x4E2D;, &#x5B9A;&#x4E49;&#x7684;&#x53C2;&#x6570;&#x79F0;&#x4E3A;<span style="color: red; font-weight: bolder;">&#x5F62;&#x5F0F;&#x53C2;&#x6570;</span>.</p>
<p>&#x5B9E;&#x9645;&#x53C2;&#x6570;, &#x987E;&#x540D;&#x601D;&#x4E49;&#x5C31;&#x662F;&#x5B9E;&#x9645;&#x7684;&#x6570;&#x636E;, &#x5373;&#x5177;&#x4F53;&#x53C2;&#x6570;&#x8FD0;&#x7B97;&#x7684;&#x6570;&#x636E;. &#x5C06;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x79F0;&#x4E3A;&#x5F62;&#x53C2;, &#x5C06;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x79F0;&#x4E3A;&#x5B9E;&#x9645;&#x53C2;&#x6570;.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">max</span>(<span class="hljs-params">num1, num2</span>) </span>{ <span class="hljs-comment">// &#x53C2;&#x4E0E;&#x51FD;&#x6570;&#x903B;&#x8F91;&#x7684;&#x7B26;&#x53F7;, num1, num2 &#x662F;&#x5F62;&#x5F0F;&#x53C2;&#x6570;</span>
    <span class="hljs-keyword">return</span> num1 &gt; num2 ? num1 : num2;
}
<span class="hljs-keyword">var</span> res1 = max(<span class="hljs-number">12</span>, <span class="hljs-number">23</span>); <span class="hljs-comment">// &#x4F20;&#x5165;&#x53C2;&#x4E0E;&#x8FD0;&#x7B97;, &#x6570;&#x5B57; 12, 23 &#x662F;&#x5B9E;&#x9645;&#x53C2;&#x6570;</span>
<span class="hljs-keyword">var</span> n1 = <span class="hljs-number">34</span>, n2 = <span class="hljs-number">45</span>;
<span class="hljs-keyword">var</span> res2 = max(n1, n2); <span class="hljs-comment">// &#x4F20;&#x5165;&#x53C2;&#x4E0E;&#x8FD0;&#x7B97;, &#x53D8;&#x91CF; n1, n2 &#x662F;&#x5B9E;&#x9645;&#x53C2;&#x6570;</span>
</code></pre>
<h3 id="%E6%B2%A1%E6%9C%89%E5%87%BD%E6%95%B0%E9%87%8D%E8%BD%BD">&#x6CA1;&#x6709;&#x51FD;&#x6570;&#x91CD;&#x8F7D;</h3>
<p>&#x5728; C++ &#x4E2D;&#x6709;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#x7684;&#x6982;&#x5FF5;, &#x6240;&#x8C13;&#x7684;&#x51FD;&#x6570;&#x91CD;&#x8F7D;, &#x662F;&#x6307;&#x5141;&#x8BB8;&#x5B9A;&#x4E49;&#x591A;&#x4E2A;&#x51FD;&#x6570;, &#x51FD;&#x6570;&#x540D;&#x76F8;&#x540C;, &#x4F46;&#x662F;&#x8981;&#x6C42;&#x53C2;&#x6570;&#x4E0D;&#x540C;. &#x90A3;&#x4E48;&#x5728; JavaScript &#x4E2D;&#x662F;&#x4E0D;&#x652F;&#x6301;&#x7684;.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x65E0;&#x53C2;&#x6570;&#x51FD;&#x6570; foo&quot;</span>);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">n</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x51FD;&#x6570;, &#x53C2;&#x6570;&#x662F; &quot;</span> + n);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">n, m</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x51FD;&#x6570;, &#x53C2;&#x6570;&#x662F; &quot;</span> + n + <span class="hljs-string">&quot; &#x548C; &quot;</span> + m);
}

foo();
foo(<span class="hljs-number">1</span>);
foo(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>);
</code></pre>
<p>&#x4ECE;&#x8BCD;&#x6CD5;&#x5206;&#x6790;&#x7684;&#x987A;&#x5E8F;&#x770B;, &#x540E;&#x9762;&#x7684;&#x51FD;&#x6570;&#x4F1A;&#x8986;&#x76D6;&#x524D;&#x9762;&#x7684;&#x51FD;&#x6570;.</p>
<h3 id="-arguments-%E5%AF%B9%E8%B1%A1"><code>arguments</code> &#x5BF9;&#x8C61;</h3>
<p>&#x51FD;&#x6570;&#x6CA1;&#x6709;&#x91CD;&#x8F7D;, &#x4F46;&#x662F;&#x5F88;&#x663E;&#x7136;, &#x524D;&#x9762;&#x4ECB;&#x7ECD;&#x7684; <code>Function</code> &#x5728;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;&#x53C2;&#x6570;. &#x90A3;&#x4E48;&#x662F;&#x5982;&#x4F55;&#x5B9E;&#x73B0;&#x7684;&#x5462;? &#x5B9E;&#x9645;&#x4E0A;&#x6BCF;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x90FD;&#x6709;&#x4E00;&#x4E2A; <code>arguments</code> &#x5BF9;&#x8C61;. &#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x50CF;&#x6570;&#x7EC4;&#x4E00;&#x6837;&#x7684;&#x5BF9;&#x8C61;, &#x63CF;&#x8FF0;&#x4E86;&#x53C2;&#x6570;&#x7684;&#x4FE1;&#x606F;&#x4E0E;&#x51FD;&#x6570;&#x7684;&#x4FE1;&#x606F;.</p>
<p>&#x4F5C;&#x4E3A;&#x5BF9;&#x8C61; <code>arguments</code> &#x6709;&#x4E0B;&#x9762;&#x5C5E;&#x6027;</p>
<ul>
<li>length + &#x7D22;&#x5F15;</li>
<li>callee</li>
</ul>
<p><code>arguments</code> &#x662F;&#x4E00;&#x4E2A;&#x50CF;&#x6570;&#x7EC4;&#x7684;&#x5BF9;&#x8C61;, &#x56E0;&#x6B64;&#x6709; <code>length</code> &#x5C5E;&#x6027;, &#x8868;&#x793A;&#x4ED6;&#x91CC;&#x9762;&#x6709;&#x591A;&#x5C11;&#x6570;&#x636E;. &#x540C;&#x65F6;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7D22;&#x5F15;&#x6765;&#x8BBF;&#x95EE;&#x6BCF;&#x4E00;&#x4E2A;&#x6570;&#x636E;:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">arguments</span>.length; i++) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>[i]);
    }
}
foo(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>);
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;</p>
<p><img src="../images/Snip20160224_30.png" alt=""></p>
<p>&#x53EF;&#x89C1;, <code>arguments</code> &#x4E2D;&#x5B58;&#x50A8;&#x7684;&#x5C31;&#x662F;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;. &#x53EF;&#x4EE5;&#x5C06;&#x5176;&#x770B;&#x6210;&#x53C2;&#x6570;&#x6570;&#x7EC4; (&#x6CE8;&#x610F;, &#x662F;&#x770B;&#x6210;&#x6570;&#x7EC4;).</p>
<p>&#x5229;&#x7528; <code>arguments</code> &#x5BF9;&#x8C61;, &#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x662F;&#x4EC0;&#x4E48;, &#x4EE5;&#x53CA;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x4E2A;&#x6570;. &#x90A3;&#x4E48;&#x6839;&#x636E;&#x4E2A;&#x6570;&#x53EF;&#x4EE5;&#x505A;&#x51FA;&#x4E0D;&#x540C;&#x7684;&#x5224;&#x65AD;, &#x5B8C;&#x6210;&#x4E0D;&#x540C;&#x7684;&#x4E8B;&#x60C5;.</p>
<p>&#x5C06;&#x524D;&#x9762;&#x7684; <code>foo</code> &#x51FD;&#x6570;&#x8FDB;&#x884C;&#x6539;&#x5199;, &#x65E2;&#x6709;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> len = <span class="hljs-built_in">arguments</span>.length;
    <span class="hljs-keyword">switch</span> (len) {
        <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>: <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x65E0;&#x53C2;&#x6570;&#x51FD;&#x6570; foo&quot;</span>); <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x51FD;&#x6570;, &#x53C2;&#x6570;&#x662F; &quot;</span> + <span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>]); <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x51FD;&#x6570;, &#x53C2;&#x6570;&#x662F; &quot;</span> +
                        <span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>] +
                        <span class="hljs-string">&quot; &#x548C; &quot;</span> +
                        <span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>]);
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">default</span>:
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x53C2;&#x6570;&#x4F9D;&#x6B21;&#x662F;: &quot;</span> + [].join.call(<span class="hljs-built_in">arguments</span>, <span class="hljs-string">&quot;, &quot;</span>));
            <span class="hljs-keyword">break</span>;
    }
}

foo();
foo(<span class="hljs-number">1</span>);
foo(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
foo(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>);
</code></pre>
<p>&#x4F8B;: &#x6C42;&#x591A;&#x4E2A;&#x6570;&#x5B57;&#x7684;&#x548C;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getSum</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> sum = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">arguments</span>.length; i++) {
        sum += <span class="hljs-built_in">arguments</span>[i];
    }
    <span class="hljs-keyword">return</span> sum;
}
</code></pre>
<p>&#x4F8B;: &#x7B80;&#x5355;&#x6A21;&#x62DF; jQuery &#x7684; onload &#x4E0E; id &#x9009;&#x62E9;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">J</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> len = <span class="hljs-built_in">arguments</span>.length,
        arg = <span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>];
    <span class="hljs-keyword">if</span> (len != <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span>;
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> (arg) === <span class="hljs-string">&quot;function&quot;</span>) {
        <span class="hljs-built_in">window</span>.onload = arg;
    }
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> (arg) === <span class="hljs-string">&quot;string&quot;</span>) {
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">document</span>.getElementById(arg);
    }
}
</code></pre>
<p>&#x7EC3;&#x4E60;: &#x6C42;&#x591A;&#x4E2A;&#x6570;&#x5B57;&#x7684;&#x6700;&#x5927;&#x503C;&#x4E0E;&#x6700;&#x5C0F;&#x503C;</p>
<p><code>callee</code> &#x662F;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x5F15;&#x7528;. &#x5373;&#x5728;&#x51FD;&#x6570; <code>foo</code> &#x5185;&#x90E8;, <code>arguments.callee</code> &#x5C31;&#x662F;&#x51FD;&#x6570; <code>foo</code> &#x672C;&#x8EAB;.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(foo === <span class="hljs-built_in">arguments</span>.callee);
}
foo(); <span class="hljs-comment">// =&gt; true</span>
</code></pre>
<p>&#x4E00;&#x822C;&#x6765;&#x8BF4;&#x7528;&#x4E0D;&#x7740;&#x8BE5;&#x5C5E;&#x6027;, &#x800C;&#x4E14;&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E2D;, <code>callee</code> &#x662F;&#x672A;&#x5B9A;&#x4E49;. &#x4F46;&#x662F;&#x4F7F;&#x7528;&#x5B83;, &#x53EF;&#x4EE5;&#x652F;&#x6301;&#x533F;&#x540D;&#x51FD;&#x6570;&#x7684;&#x9012;&#x5F52;.</p>
<h2 id="%E5%87%BD%E6%95%B0%E5%90%8D%E6%8F%90%E5%8D%87">&#x51FD;&#x6570;&#x540D;&#x63D0;&#x5347;</h2>
<p>&#x51FD;&#x6570;&#x6709;&#x5B9A;&#x4E49;&#x8BED;&#x6CD5;, &#x4E5F;&#x6709;&#x5B57;&#x9762;&#x91CF;&#x8BED;&#x6CD5;. &#x867D;&#x7136;&#x51E0;&#x4E4E;&#x662F;&#x4E00;&#x6837;&#x7684;, &#x4F46;&#x662F;&#x8FD8;&#x662F;&#x6709;&#x4E00;&#x70B9;&#x70B9;&#x7684;&#x5F02;&#x540C;. &#x5148;&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x6848;&#x4F8B;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(num);
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">456</span>;
    <span class="hljs-built_in">console</span>.log(num);
}
foo();
</code></pre>
<p>&#x539F;&#x56E0;&#x662F;, &#x5728; javascript &#x4E2D;, js &#x5F15;&#x64CE;&#x5728;&#x89E3;&#x91CA;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;, &#x4F1A;&#x5C06;&#x6240;&#x6709;&#x7684;&#x53D8;&#x91CF;&#x58F0;&#x660E;&#x63D0;&#x5347;&#x5230;&#x6700;&#x524D;&#x9762;. &#x56E0;&#x6B64;&#x5728;&#x51FD;&#x6570; <code>foo</code> &#x91CC;&#x9762;, &#x4EE3;&#x7801;&#x5C31;&#x4F1A;&#x53D8;&#x6210;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num;
    <span class="hljs-built_in">console</span>.log(num);
    num = <span class="hljs-number">456</span>;
    <span class="hljs-built_in">console</span>.log(num);
}
foo();
</code></pre>
<p>&#x6253;&#x5370;&#x672A;&#x521D;&#x59CB;&#x5316;&#x7684;&#x53D8;&#x91CF;, &#x5F53;&#x7136;&#x662F; <code>undefined</code>. &#x8FD9;&#x6837;&#x867D;&#x7136;&#x53D8;&#x91CF;&#x540D;&#x5B9A;&#x4E49;&#x5728;&#x4E0B;&#x9762;, &#x4F46;&#x662F;&#x611F;&#x89C9;&#x597D;&#x50CF;&#x5C06;&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x5728;&#x524D;&#x9762;&#x4E00;&#x6837;. &#x8FD9;&#x4E2A;&#x7684;&#x7ED3;&#x8BBA;&#x6211;&#x4EEC;&#x6210;&#x4E3A; &quot;&#x53D8;&#x91CF;&#x540D;&#x63D0;&#x5347;&quot;. &#x540C;&#x6837;&#x6211;&#x4EEC;&#x8BF4;&#x8FC7;: &#x51FD;&#x6570;&#x4E5F;&#x662F;&#x4E00;&#x4E2D;&#x6570;&#x636E;&#x7C7B;&#x578B;, &#x548C;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x4E00;&#x6837;. &#x56E0;&#x6B64;&#x5B58;&#x50A8;&#x53D8;&#x91CF;&#x540D;&#x63D0;&#x5347;, &#x4E5F;&#x5B58;&#x5728;&#x51FD;&#x6570;&#x540D;&#x63D0;&#x5347;.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> foo = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;foo&quot;</span>);
}

<span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    foo();
    <span class="hljs-keyword">var</span> foo = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;func.foo&quot;</span>);
    }
}

func();
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x662F;&#x629B;&#x51FA;&#x5F02;&#x5E38;. &#x539F;&#x56E0;&#x5F88;&#x7B80;&#x5355;, &#x548C;&#x53D8;&#x91CF;&#x540D;&#x63D0;&#x5347;&#x4E00;&#x6837;, &#x51FD;&#x6570;&#x540D;&#x4E5F;&#x4F1A;&#x63D0;&#x5347;, &#x4FEE;&#x6539;&#x4E00;&#x4E0B;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> foo = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;foo&quot;</span>);
}

<span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> foo;
    foo();
    foo = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;func.foo&quot;</span>);
    }
}

func();
</code></pre>
<p>&#x5F88;&#x660E;&#x663E;, <code>foo</code> &#x662F; <code>undefined</code>, &#x81EA;&#x7136;&#x4E5F;&#x5C31;&#x4E0D;&#x80FD;&#x5F53;&#x505A;&#x51FD;&#x6570;&#x6765;&#x8C03;&#x7528;. &#x6240;&#x4EE5;&#x51FA;&#x73B0;&#x9519;&#x8BEF;.</p>
<p>&#x90A3;&#x4E48;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x7684;&#x58F0;&#x660E;&#x8BED;&#x6CD5;&#x5462;? &#x90A3;&#x5C31;&#x4F1A;&#x6B63;&#x5E38;&#x6267;&#x884C;. &#x539F;&#x56E0;&#x662F;&#x5728; js &#x5F15;&#x64CE;&#x89E3;&#x6790;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;. &#x9996;&#x5148;&#x4F1A;&#x5C06;&#x51FD;&#x6570;&#x7684;&#x58F0;&#x660E;&#x8BED;&#x6CD5;&#x5168;&#x90E8;&#x52A0;&#x8F7D;&#x4E00;&#x904D;, &#x56E0;&#x6B64;&#x5728;&#x6267;&#x884C;&#x8D4B;&#x503C;&#x8BED;&#x53E5;&#x7684;&#x65F6;&#x5019;, &#x5185;&#x5B58;&#x4E2D;&#x65E9;&#x5DF2;&#x6709;&#x51FD;&#x6570;&#x7684;&#x58F0;&#x660E;&#x4E86;. &#x56E0;&#x6B64;&#x8C03;&#x7528;&#x5C31;&#x4E0D;&#x4F1A;&#x51FA;&#x9519;&#x5566;.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;foo&quot;</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">func</span>(<span class="hljs-params"></span>) </span>{
    foo();
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;func.foo&quot;</span>);
    }
}

func();
</code></pre>
<p>&#x7531;&#x4E8E;&#x6709;&#x4E86;&#x8FD9;&#x4E9B;&#x4E0D;&#x540C;, &#x56E0;&#x6B64;&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x7684;&#x65F6;&#x5019;, &#x63A8;&#x8350;&#x5C06;&#x53D8;&#x91CF;&#x90FD;&#x5199;&#x5728;&#x5F00;&#x59CB;&#x7684;&#x5730;&#x65B9;, &#x4E5F;&#x5C31;&#x662F;&#x5728;&#x51FD;&#x6570;&#x7684;&#x5F00;&#x5934;&#x5C06;&#x53D8;&#x91CF;&#x5C31;&#x5B9A;&#x4E49;&#x597D;, &#x7C7B;&#x4F3C;&#x4E8E; C &#x8BED;&#x8A00;&#x7684;&#x89C4;&#x5B9A;&#x4E00;&#x6837;. &#x8FD9;&#x4E2A;&#x5728; js &#x5E93;&#x4E2D;&#x4E5F;&#x662F;&#x8FD9;&#x4E48;&#x5B8C;&#x6210;&#x7684;, &#x5982; jQuery &#x7B49;.</p>
<p>&#x95EE;&#x9898;: &#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x662F;&#x4EC0;&#x4E48;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// 1.</span>
<span class="hljs-keyword">var</span> a = <span class="hljs-number">123</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">a</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;a function&quot;</span>);
}
<span class="hljs-built_in">console</span>.log(a);

<span class="hljs-comment">// 2.</span>
b();
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">b</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;b function&quot;</span>);
}

<span class="hljs-comment">// 3.</span>
c();
<span class="hljs-keyword">var</span> c = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;c function&quot;</span>);
};
</code></pre>
<h2 id="-eval-%E5%87%BD%E6%95%B0%E4%B8%8E-function-%E5%87%BD%E6%95%B0"><code>eval</code> &#x51FD;&#x6570;&#x4E0E; <code>Function</code> &#x51FD;&#x6570;</h2>
<p>&#x5728; javascript &#x6709;&#x4E00;&#x4E2A;&#x51FD;&#x6570; <code>eval</code> &#x975E;&#x5E38;&#x5F3A;&#x5927;. &#x5B83;&#x53EF;&#x4EE5;&#x52A8;&#x6001;&#x7684;&#x6267;&#x884C;&#x811A;&#x672C;. &#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x5165;, &#x7136;&#x540E;&#x51FD;&#x6570;&#x6267;&#x884C;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x4F1A;&#x5C06;&#x8FD9;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x89E3;&#x6790;&#x6210;&#x811A;&#x672C;&#x6267;&#x884C;. &#x4F8B;&#x5982;:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(num); <span class="hljs-comment">// =&gt; &#x62A5;&#x9519;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x52A0;&#x4E0A; <code>eval</code> &#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">eval</span>(<span class="hljs-string">&quot;var num = 123;&quot;</span>);
<span class="hljs-built_in">console</span>.log(num); <span class="hljs-comment">// =&gt; 123;</span>
</code></pre>
<p>&#x53EF;&#x89C1; <code>eval</code> &#x7684;&#x529F;&#x80FD;&#x975E;&#x5E38;&#x5F3A;&#x5927;. &#x5F53;&#x7136;&#x4E5F;&#x6709;&#x4EBA;&#x4F1A;&#x8BA4;&#x4E3A;&#x8FD9;&#x4E2A;&#x5199;&#x6CD5;&#x5B8C;&#x5168;&#x662F;&#x6D6A;&#x8D39;&#x8868;&#x60C5;. &#x5F53;&#x7136;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#x4E0D;&#x4F1A;&#x8FD9;&#x4E48;&#x4F7F;&#x7528;. &#x8FD9;&#x91CC;&#x53EA;&#x662F;&#x4E3A;&#x4E86;&#x8BF4;&#x660E;&#x5B83;&#x7684;&#x57FA;&#x672C;&#x65B9;&#x6CD5;&#x800C;&#x5DF2;. &#x90A3;&#x4E48;&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#x53EF;&#x4EE5;&#x5229;&#x7528;&#x5B83;&#x5B9E;&#x73B0;&#x4E00;&#x4E9B;&#x8F83;&#x4E3A;&#x7075;&#x6D3B;&#x7684;&#x529F;&#x80FD;. &#x4F8B;&#x5982;&#x7ED8;&#x5236;&#x51FD;&#x6570;&#x66F2;&#x7EBF;. &#x53EF;&#x4EE5;&#x6839;&#x636E;&#x7528;&#x6237;&#x7684;&#x8F93;&#x5165;&#x51B3;&#x5B9A;&#x51FD;&#x6570;&#x662F;&#x4EC0;&#x4E48;, &#x7136;&#x540E;&#x518D;&#x8FDB;&#x884C;&#x7ED8;&#x5236;. &#x518D;&#x6BD4;&#x5982;&#x5229;&#x7528; ajax &#x83B7;&#x5F97;&#x6570;&#x636E;. &#x90A3;&#x4E48;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#x4E00;&#x822C;&#x5F97;&#x5230;&#x7684;&#x662F; json &#x683C;&#x5F0F;&#x7684;&#x5B57;&#x7B26;&#x4E32;, &#x90A3;&#x4E48;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8BE5;&#x51FD;&#x6570;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x53D8;&#x6210;&#x5BF9;&#x8C61;.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> txt = <span class="hljs-string">&apos;{name: &quot;&#x848B;&#x5764;&quot;, course: &quot;JavaScript &#x9762;&#x5411;&#x5BF9;&#x8C61;&#x9AD8;&#x7EA7;&quot;}&apos;</span>;
<span class="hljs-keyword">var</span> o = <span class="hljs-built_in">eval</span>(<span class="hljs-string">&quot;(&quot;</span> + txt + <span class="hljs-string">&quot;)&quot;</span>);
</code></pre>
<p>&#x5F53;&#x7136;&#x8FDC;&#x8FDC;&#x8FD9;&#x4E48;&#x4E9B;, &#x51E1;&#x662F;&#x52A8;&#x6001;&#x5904;&#x7406;&#x7684;&#x4E1C;&#x897F;&#x90FD;&#x53EF;&#x4EE5;&#x5229;&#x7528;&#x5B83;&#x6765;&#x5B9E;&#x73B0;.</p>
<p>&#x4F46;&#x662F; <code>eval</code> &#x51FD;&#x6570;&#x4E5F;&#x6709;&#x5B83;&#x7684;&#x5F0A;&#x7AEF;. &#x56E0;&#x4E3A;&#x5B83;&#x4F1A;&#x4E0D;&#x7ECF;&#x610F;&#x7684;&#x6C61;&#x67D3;&#x5168;&#x5C40;&#x53D8;&#x91CF;. &#x56E0;&#x6B64;&#x4E00;&#x822C;&#x5F00;&#x53D1;&#x4E2D;&#x63A8;&#x8350;&#x4F7F;&#x7528; <code>Function</code> &#x6765;&#x4EE3;&#x66FF; <code>eval</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> txt = <span class="hljs-string">&apos;{name: &quot;&#x848B;&#x5764;&quot;, course: &quot;JavaScript &#x9762;&#x5411;&#x5BF9;&#x8C61;&#x9AD8;&#x7EA7;&quot;}&apos;</span>;
<span class="hljs-keyword">var</span> o = (<span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>(<span class="hljs-string">&quot;return &quot;</span> + txt))();
</code></pre>
<h2 id="%E5%B0%8F%E7%BB%93">&#x5C0F;&#x7ED3;</h2>
<ul>
<li>&#x51FD;&#x6570;&#x4E5F;&#x662F;&#x5BF9;&#x8C61;, &#x662F; <code>Function</code> &#x7684;&#x5B9E;&#x4F8B;</li>
<li>Function &#x65E2;&#x662F;&#x51FD;&#x6570;, &#x53C8;&#x662F;&#x5BF9;&#x8C61;</li>
<li><code>Function.prototype</code> &#x662F;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x7684;&#x539F;&#x578B;</li>
<li><code>arguments</code> &#x5BF9;&#x8C61;</li>
<li>&#x51FD;&#x6570;&#x540D;&#x63D0;&#x5347;</li>
<li>&#x4F7F;&#x7528; <code>eval</code> &#x548C; <code>Function</code> &#x52A8;&#x6001;&#x7684;&#x6267;&#x884C;&#x811A;&#x672C;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html" class="navigation navigation-prev " aria-label="Previous page: Object 对象"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html" class="navigation navigation-next " aria-label="Next page: 作用域链"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
